package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.newProjectCenter.bean.PjMeterMaterialBean;
import com.koron.css2.newProjectCenter.bean.query.PjMeterMaterialQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjMeterMaterialMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 工程材料清单导出
 */
public class PjMeterMaterialExport extends AbstractExportExcel {

    private static Logger logger = LoggerFactory.getLogger(PjMeterMaterialExport.class);

    @Override
    public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        List<PjMeterMaterialBean> pjMaterialList = new ArrayList<>();
        try {
            PjMeterMaterialQueryBean pjMaterialQuery = JsonUtils.objectToPojo(req.getData(), PjMeterMaterialQueryBean.class);
            PjMeterMaterialMapper pjMaterialMapper = factory.getMapper(PjMeterMaterialMapper.class);
            pjMaterialList = pjMaterialMapper.selectByPjId(pjMaterialQuery.getPjId());
            for (PjMeterMaterialBean pjMeterMaterial :  pjMaterialList){
                pjMeterMaterial.setMaterialTypeName(pjMeterMaterial.getMaterialType() == PjMeterMaterialBean.MATER_TYPE_AFTER ? "表后材料" : "表前材料");
            }
        }catch (Exception ex){
            logger.error("工程材料列表数据查询失败:" + ex);
        }
        AttachmentDto attachment = new AttachmentDto();
        attachment.setBtlTemplate("PjMeterMaterialExport.btl");
        attachment.setData(pjMaterialList);
        attachment.setFilename("工程材料导出");
        attachment.setFileTemplate("PjMeterMaterialExport.xlsx");
        attachment.setFileType("excel");
        return attachment;
    }
}
